export default function generator(ast) {
    let renderStr = generateElement(ast)
    // console.log(renderStr)
    // 使用new Funtion 生成渲染器
    return new Function(`with(this) { return ${renderStr} }`)
}

function generateElement(ast) {
    const { attr, rawAttr, tag } = ast
    let attrs = { ...rawAttr, ...attr }
    let children = getChildren(ast)
    return `_c('${tag}', ${JSON.stringify(attrs)}, [${children}])`
}

function getChildren(ast) {
    const { children } = ast, ret = []
    for (let i = 0, len = children.length; i < len; i++) {
        if (children[i].type === 1) {
            // 元素节点
            ret.push(generateElement(children[i]))
        } else if (children[i].type === 3) {
            // 文本节点
            ret.push(`_v(${JSON.stringify(children[i])})`)
        }
    }
    return ret
}